#!/usr/bin/perl

$argc = $#ARGV+1;
print STDERR "Number of command line args = $argc\n";
if ($argc != 5) {
    print STDERR "ERROR: incorrect number of command line args ($argc/5)\n";
    print STDERR "\n";
    print STDERR "Usage => makeFoldPlot.pl [fold file name] [foldfreq] [fold amp] [fold phase] [fold dc amp]\n";
    print STDERR "\n";
    exit(1);
}

$DATAFILE   = $ARGV[0]; 
$FREQ       = $ARGV[1];
$AMP        = $ARGV[2];
$PHASE      = $ARGV[3];
$DC         = $ARGV[4];
$PLOTFILE = "${DATAFILE}.ps";

print STDERR "==========================================================\n";
print STDERR "datafile   =  $DATAFILE\n";
print STDERR "plotfile   =  $PLOTFILE\n";
print STDERR "fold freq  =  $FREQ\n";
print STDERR "fold amp   =  $AMP\n";
print STDERR "fold phase =  $PHASE\n";
print STDERR "dc   amp   =  $DC\n";
print STDERR "==========================================================\n";	

$gnuplotScript = "plot_f${FREQ}_a${AMP}_p${PHASE}.plot";
open(PLOT,">$gnuplotScript");
print STDERR "GNUplot script file = $gnuplotScript\n";

# ---------------------------------------------------------------------------------------------
print PLOT "reset \n";
print PLOT "set terminal postscript landscape enhanced color dashed lw 1 \"Helvetica\" 11 \n";
print PLOT "set output \"my-plot.ps\" \n";
print PLOT "unset key \n";
print PLOT "set key spacing 2 \n";
print PLOT "unset label \n";
print PLOT "set grid xtics ytics \n";
print PLOT "set xtics 0.1 \n";
print PLOT "set mxtics 2 \n";
print PLOT "set mytics 2 \n";
print PLOT "set ylabel \"mean amp.\" \n";
print PLOT "set xlabel \"phase bin (mid phase)\" \n";
print PLOT "\n";
print PLOT "set title \"folded speed data [${DATAFILE}] (freq = ${FREQ})\" font \"Helvetica,16\" \n";
print PLOT "\n";
print PLOT "plot \"${DATAFILE}\" using 2:3:4 with yerr \\";                 print PLOT "\n"; 
print PLOT "     title \"${DATAFILE}\", \\";                                print PLOT "\n";
print PLOT "     \"${DATAFILE}\" using 2:3:(1E8*\$4) smooth acsplines \\";  print PLOT "\n";
print PLOT "     title \"acsplines fit\" with lines, \\";                   print PLOT "\n";
print PLOT "     ${AMP}*cos(2*pi*x+${PHASE}*pi)+${DC} \\";                  print PLOT "\n";
print PLOT "     title \"${AMP}*cos(2*pi*x+${PHASE}*pi)+${DC}\" \n";
print PLOT "      \n";
print PLOT "set terminal x11 \n";
print PLOT "!mv my-plot.ps $PLOTFILE \n";
# ---------------------------------------------------------------------------------------------



print STDERR "=================================================\n\n";
print STDERR "emacs -nw $gnuplotScript\n";
print STDERR "gnuplot $gnuplotScript\n";
print STDERR "gv $PLOTFILE\n\n";
